<?php

namespace app\admin\controller;

use app\common\model\CourseCate;

class Courses extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }
    /**
     * @route('courses/coursecatelist')
     * 课程分类列表
     */
    public function CourseCateList(){
        $isdisplay = false;
        // 获取搜索条件
        $get['ac_name'] = input('get.ac_name');
        $get['is_del'] = 1;
        $field = 'ac_id,parent_id,ac_name,sort,is_del,type';
        $order = 'parent_id asc,sort asc';

        $get['shop_id'] = 0;
        $Model = new CourseCate();
        $fdata = $Model->get_tree($field,$get,$order);

        $data = [];
        foreach ($fdata as $key => $value){
            if(array_key_exists("ac_name", $value)){
                $data[] = [
                    'ac_id' => $value['ac_id'],
                    'parent_id' => $value['parent_id'],
                    'ac_name' => $value['ac_name'],
                    'sort' => $value['sort'],
                    'type' => $value['type'],
                ];

                if(count($value['children']) > 0){
                    foreach ($value['children'] as $k => $v){
                        $data[] = [
                            'ac_id' => $v['ac_id'],
                            'parent_id' => $v['parent_id'],
                            'ac_name' => $v['ac_name'],
                            'sort' => $v['sort'],
                            'type' => $v['type'],
                        ];
                    }
                }
            }

        }

        $tvar = array(
            "title" => "课程分类列表",
            "time"  => time(),
            "data"  => $data,
            "get"   => $get,
            "isdisplay" => $isdisplay,
        );
//		print_r($tvar);die;
        $this->assign("data", $tvar);
        return $this->fetch('courses/cate/coursecatelist');
    }


    /**
     * 添加课程分类
     * @route('courses/boxcoursecate')
     */
    public function BoxCourseCate()
    {
        $id = input('id');
        $CourseCate = new CourseCate();

        if(request()->isAjax()){
            $param = input();

            $params['ac_name'] = $param['ac_name'];
            $params['sort'] = $param['sort'];
            $params['top_pic'] = $param['imageurl'];
            $params['admin_id'] = $this->_userid;

            $rs = $CourseCate->edit($params, $param['id']);
            if($rs === false){
                $this->error('操作失败');
            }
            $this->success('操作成功');
        }

        $cond['parent_id'] = 0;
        $type_data = $CourseCate->get_list('ac_id, ac_name', $cond,'sort asc');

        $data = null;
        if($id > 0){
            $data = $CourseCate->getOne(['ac_id' => $id,'is_del' => 1],'ac_id,parent_id,ac_name,sort,top_pic');
        }

        $tvar = array(
            "title" => "添加课程分类",
            "time"  => time(),
            'type_data' => $type_data,
            "id" => $id,
            "data" => $data
        );
        $this->assign("data", $tvar);
        return $this->fetch('courses/cate/boxcoursecate');
    }

    /**
     * 删除分类
     * @route('courses/delcoursecate')
     */
    public function DelCourseCate()
    {
        $id = input('id');

        // 软删除 父级+子级一起删除  （十一）
        $article = CourseCate::get($id,'subs');
        $rs = $article->together('subs')->delete();

        if($rs === false){
            $this->error('操作失败');
        }
        $this->success('操作成功');

    }

    /**
     * 课程分类图片上传
     * @route('courses/coursesupload')
     */
    public function coursesupload()
    {
        $res = \Upload::FileUpload('file','course',1024*1024*10);
        if(!$res['data']){
            $this->result([],-1,$res['msg']);
        }
        $this->result($res['data']['file_path'],1);
    }
}